<script setup>
import { ref } from 'vue';
import { getBannerAPI, getRecommendAPI, getNoticeAPI, getClassifyAPI } from '@/api/api';
import {onReady, onShareAppMessage, onShareTimeline} from '@dcloudio/uni-app'
 
	const bannerList = ref([])
	const recommendList = ref([])
	const noticeList = ref([])
	const classifyList = ref([])
	async  function getBanner() {
		const res = await getBannerAPI()
		// console.log(res);
		bannerList.value = res.data
	}
	
	async function getRecommend() {
		const res = await getRecommendAPI()
		// console.log(res);
		recommendList.value = res.data
	}
	
	async function getNotice() {
		const res = await getNoticeAPI({select: true})
		// console.log(res);
		noticeList.value = res.data
	}
	
	async function getClassify() {
		const res = await getClassifyAPI({select: true})
		// console.log(res);
		classifyList.value = res.data
	}
	onReady(() => {
		getBanner()
		getRecommend()
		getNotice()
		getClassify()
	})
	
	
	// 去预览页
	function goPreview(item) {
		uni.setStorageSync('classListStorage', recommendList.value)
		uni.navigateTo({
			url: `/pages/preview/preview?id=${item._id}`
		})
	}
	
	// 分享给好友
	onShareAppMessage((e) => {
		return {
			title: '岁月壁纸',
			path: '/pages/index/index',
		}
	})
	
	// 分享朋友圈
	onShareAppMessage((e) => {
		console.log(e);
		return {
			title: '岁月壁纸'
		}
	})
	
	function goClassList(url) {
		uni.navigateTo({
			url: `/pages/classList/classList?${url}`
		})
	}
	
	function goClassList2(url) {
		uni.navigateTo({
			url: url
		})
	}
</script>


<template>
	<view class="home-layout pageBg">
		<custom-nav-bar title="推荐"></custom-nav-bar>
		<view class="banner">
			<swiper :autoplay="true" :interval="3000" :circular="true" :indicator-dots="true" :indicator-active-color="'#fff'" :indicator-color="'rgba(255, 255, 255, .5)'">
				<swiper-item v-for="item in bannerList" :key="item._id">
					<navigator v-if="item.target == 'self'" class="nav" :url="`/pages/classList/classList?${item.url}`">
						<image :src="item.picurl" mode="widthFix"></image>
					</navigator>
					
					<navigator v-else target="miniProgram" :app-id="item.appid" class="nav" :url="item.url">
						<image :src="item.picurl" mode="widthFix"></image>
					</navigator>
				</swiper-item>
				
			</swiper>
		</view>
		<view class="notice">
			<view class="left">
				<uni-icons type="sound-filled" size="20"></uni-icons>
				<text>公告</text>
			</view>
			<view class="center">
				<swiper autoplay vertical interval="3000" circular>
					<swiper-item v-for="item in noticeList" :key="item._id">
						<navigator :url="`/pages/notice/detail?id=65360ea7bd0220d7635bd34b`">反馈问题</navigator>
					</swiper-item>
				</swiper>
			</view>
			<view class="right">
				<uni-icons type="right" size="20"></uni-icons>
			</view>
		</view>
		<view class="select">
			<commont-title name="每日推荐">
				<view class="date">
					<uni-icons type="calendar" size="18"></uni-icons>
					<uni-dateformat :date="Date.now()" format="dd"></uni-dateformat>
					日
				</view>
			</commont-title>
			<view class="content">
				<scroll-view scroll-x="true">
					<view @click="goPreview(item)" class="box" v-for="item in recommendList" :key="item._id">
						<image :src="item.smallPicurl" mode="aspectFill"></image>
					</view>
				</scroll-view>
			</view>
		</view>
		<view class="theme">
			<commont-title name="专题精选">
				<navigator url="/pages/classify/classify" open-type="reLaunch" class="more">More+</navigator>
			</commont-title>
			<view class="content">
				<theme-item v-for="item in classifyList" :item="item" :key="item._id"></theme-item>
				<theme-item :more="true"></theme-item>
			</view>
		</view>
	</view>
</template>


<style scoped lang='scss'>
	.home-layout{
		.banner{
			width: 750rpx;
			padding: 30rpx 0;
			swiper{
				width: 750rpx;
				height: 340rpx;
				swiper-item{
					width: 100%;
					height: 100%;
					padding: 0 30rpx;
					.nav{
						width: 100%;
						height: 100%;
					}
					image{
						width: 100%;
						height: 100%;
						border-radius: 10rpx;
					}
				}
			}
		}
		.notice{
			display: flex;
			width: 690rpx;
			height: 80rpx;
			line-height: 80rpx;
			margin: 0 auto;
			background-color: #f9f9f9;
			border-radius: 80rpx;
			.left{
				width: 140rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				:deep() {
					.uni-icons{
						color: $brand-theme-color !important;
					}
				}
				
				text{
					font-size: 28rpx;
					font-weight: bold;
					color: $brand-theme-color;
				}
			}
			.center{
				flex: 1;
				swiper{
					height: 100%;
					swiper-item{
						color: #666;
						font-size: 30rpx;
						overflow: hidden;
						white-space: nowrap;
						text-overflow: ellipsis;
					}
				}
			}
			.right{
				display: flex;
				justify-content: center;
				align-items: center;
				width: 70rpx;
			}
		}
		.select{
			margin-top: 50rpx;
			.date{
				display: flex;
				align-items: center;
				color: $brand-theme-color;
				:deep() {
					.uni-icons{
						color: $brand-theme-color !important;
					}
				}
				text{
					margin-left: 5rpx;
				}
			}
			.content{
				width: 720rpx;
				margin-top: 30rpx;
				margin-left: 30rpx;
				scroll-view{
					white-space: nowrap;
					.box{
						display: inline-block;
						width: 200rpx;
						height: 440rpx;
						margin-right: 15rpx;
						image{
							width: 100%;
							height: 100%;
							border-radius: 8rpx;
						}
						&:last-child{
							margin-right: 30rpx;
						}
					}
				}
			}
			
		}
		
		.theme{
			padding: 50rpx 0;
			.more{
				color: #888;
				font-size: 32rpx;
			}
			.content{
				margin-top: 30rpx;
				padding: 0 30rpx;
				display: grid;
				grid-template-columns: repeat(3, 1fr);
				gap: 15rpx;
			}
		}
	}
</style>